/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import fr.ove.utils.Connection;
import fr.ove.utils.ConnectionConfigurable;

/**
* A connection parameter setter.
*/
public class ConnectionHandlerDialog extends JDialog {
    /**
    * The connection.
    */
    private Connection connection; 
    
    /**
    * A connection configurable dialog.
    */
    private ConnectionConfigDialog connectionConfigDialog;
    
    /**
    * A message status for the connection.
    */
    private JLabel status;
    
    /**
    * The open connection button.
    */
    private JButton buttonConnect;
    
    /**
    * The close connection button.
    */
    private JButton buttonDisconnect;
    
    /**
    * The configure connection button.
    */
    private JButton buttonConfig;
    
    /**
    * The connexion satus : online
    */
    private ImageIcon online;
    
    /**
    * The connexion satus : offline
    */
    private ImageIcon offline;
    
    private JFrame frame;
    
    private StatusBar statusBar;
    
    /**
    * The Constructor.
    * @param configurableObj a connection configurable object.
    */
    public ConnectionHandlerDialog(JFrame parentFrame, StatusBar statusBar) {
        super(parentFrame, "Connection Handler", true);
        
        frame = parentFrame;
        this.statusBar = statusBar;
        
        connectionConfigDialog = new ConnectionConfigDialog(frame, null);
        
        JPanel contentPane = new JPanel(new GridBagLayout());
        
        EtchedBorder border = (EtchedBorder) BorderFactory.createEtchedBorder();
        contentPane.setBorder(border);
        
        online = getIcon("fr/ove/openmath/mathematica/images/online.gif");
        offline = getIcon("fr/ove/openmath/mathematica/images/offline.gif");
        
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		
        status = new JLabel();
        status.setIconTextGap(15);
        status.setHorizontalAlignment(SwingConstants.CENTER);
        constraints.gridwidth = 3;
        constraints.ipadx = 40;
        constraints.ipady = 40;
        contentPane.add(status, constraints);
        
        buttonConnect = new JButton("Connect");
        buttonConnect.setToolTipText("Open the connection");
        buttonConnect.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    try {
                        connection.open();
                        ConnectionHandlerDialog.this.statusBar.setConnectionStatus(true);
                        String text = "Connected to [" + connection.getHostName() + ", " +
                                        connection.getPortNumber() + "]";
                        status.setText(text);
                        status.setIcon(online);
                        
                        buttonConnect.setEnabled(false);
                        buttonDisconnect.setEnabled(true);
                        buttonConfig.setEnabled(false);
                    }
                    catch (IOException ioe) {
                        String errorMsg = "failed to open a connection to [" + 
                                        connection.getHostName() + ", " + connection.getPortNumber() +
                                        "]";
                        JOptionPane jop = new JOptionPane(errorMsg, JOptionPane.ERROR_MESSAGE);
                        JDialog dialog = jop.createDialog(frame, "Connection Failed");
                        dialog.show();
                    }
                }
            }
        );
        constraints.gridwidth = 1;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.ipady = 0;
        constraints.insets = new Insets(0, 10, 10, 2);
        contentPane.add(buttonConnect, constraints);
        
        buttonDisconnect = new JButton("Disconnect");
        buttonDisconnect.setToolTipText("Close the connection");
        buttonDisconnect.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    try {
                        connection.close();
                        ConnectionHandlerDialog.this.statusBar.setConnectionStatus(false);
                        String text = "Disconnected from [" + connection.getHostName() + ", " +
                                        connection.getPortNumber() + "]";
                        status.setText(text);
                        status.setIcon(offline);
                        
                        buttonConnect.setEnabled(true);
                        buttonDisconnect.setEnabled(false);
                        buttonConfig.setEnabled(true);
                    }
                    catch (IOException ioe) {
                    }
                }
            }
        );
        constraints.gridx = 1;
        constraints.insets = new Insets(0, 0, 10, 0);
        contentPane.add(buttonDisconnect, constraints);
        
        buttonConfig = new JButton("Configure");
        buttonConfig.setToolTipText("Configure the connection");
        buttonConfig.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    connectionConfigDialog.setConnectionConfigurable(connection);
                    connectionConfigDialog.setLocationRelativeTo(ConnectionHandlerDialog.this);
                    connectionConfigDialog.show();
                    String text = "Disconnected from [" + connection.getHostName() + ", " +
                            connection.getPortNumber() + "]";
                    status.setText(text);
                }
            }
        );
        constraints.gridx = 2;
        constraints.insets = new Insets(2, 0, 10, 10);
        contentPane.add(buttonConfig, constraints);
        
		setContentPane(contentPane);
    }
    
    /**
    * sets the connection to handle.
    * @param connection the connection to handle.
    */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    
    /**
    * Shows the dialog
    */
    public void show() {
        if (connection.isOpened()) {
            String text = "Connected to [" + connection.getHostName() + ", " +
                            connection.getPortNumber() + "]";
            status.setText(text);
            status.setIcon(online);
            
            buttonConnect.setEnabled(false);
            buttonDisconnect.setEnabled(true);
            buttonConfig.setEnabled(false);
        }
        else {
            String text = "Disconnected from [" + connection.getHostName() + ", " +
                            connection.getPortNumber() + "]";
            status.setText(text);
            status.setIcon(offline);
            
            buttonConnect.setEnabled(true);
            buttonDisconnect.setEnabled(false);
            buttonConfig.setEnabled(true);
        }
        
        pack();
        setLocationRelativeTo(frame);
        super.show();
    }
    
    /**
    * Returns the icon with the specified image filename.
    * @param filename the image filename
    */
    private ImageIcon getIcon(String filename) {
        ImageIcon icon = null;
        
        java.net.URL iconURL = ClassLoader.getSystemResource(filename);
        if (iconURL != null)
            icon = new ImageIcon(iconURL);
            
        return icon;
    }
}
